<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="btn">发Ajax请求</button>
  <script>
    const btn = document.querySelector('#btn')
    btn.addEventListener('click',function(){
      // 创建Promise
      const p = new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest()
        xhr.open('GET','http://localhost:3000/list')
        xhr.send()
        xhr.onreadystatechange = function(){
          if(xhr.readyState === 4){
            if(xhr.status >= 200 && xhr.status < 300){
              // 控制台输出响应体
              resolve(xhr.response)
            }else{
              // 控制台输出响应状态码
              reject(xhr.status)
            }
          }
        }
      })
      p.then((data)=>{
        console.log(data)
      },(error)=>{
        console.log(error)
      })
    })
  </script>
</body>
</html>